home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / StdIO / fopen.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  2KB  |  90 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /*  fopen.c - assigns buffered I/O streams to files
  18.  *
  19.  *  fopen - opens the given file name for buffered I/O
  20.  *  freopen - changes the mode on a buffered I/O stream  
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26.  
  27. extern int errno;
  28. extern void fclose();
  29. extern int  open(), strcmp();
  30. extern FILE *addStream();
  31.  
  32. static struct modeType {
  33.     char *requested;
  34.     int   openMode;
  35.     } modeTable[9] = {
  36.         {"r",   O_RDONLY},
  37.         {"r+",  O_RDWR},
  38.         {"w",  (O_WRONLY|O_CREAT|O_TRUNC)},
  39.         {"w+", (O_RDWR|  O_CREAT|O_TRUNC)},
  40.         {"a",  (O_WRONLY|O_CREAT|O_APPEND)},
  41.         {"a+", (O_RDWR|  O_CREAT|O_APPEND)},
  42.         {"x",  (O_WRONLY|O_CREAT|O_EXCL)},
  43.         {"x+", (O_RDWR|  O_CREAT|O_EXCL)},
  44.         {"",    0L}
  45.         };
  46.  
  47. FILE *
  48. freopen(name, mode, fp)
  49. char *name;
  50. char *mode;
  51. FILE *fp;
  52. {
  53.     struct modeType *mp;
  54.     int     fd;
  55.  
  56.     fclose(fp);
  57.     /*
  58.      * Set mp to point to the correct table entry.
  59.      */
  60.     for (mp = &modeTable[0];; ++mp) {
  61.         if ( !*mp->requested ) {
  62.             errno = EINVAL;
  63.             return NULL;
  64.         }
  65.         if ( strcmp(mp->requested, mode) == 0)
  66.             break;
  67.     }
  68.     /*
  69.      * Open it again with the correct attributes.
  70.      */
  71.     if (( fd = open(name, mp->openMode)) == -1)
  72.         return NULL;
  73.     fp->_fileunit = fd;
  74.     fp->_fileflag = _FILEACTIVE;
  75.     return fp;
  76. }
  77.  
  78. FILE *
  79. fopen(name, mode)
  80. char *name;
  81. char *mode;
  82. {
  83.     FILE *fp;
  84.  
  85.     if ((fp = addStream()) == NULL )
  86.         return NULL;
  87.     return freopen( name, mode, fp );
  88. }
  89.  
  90.